package com.ruoyi.web.controller.wms;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.wms.domain.WmsCustomer;
import com.ruoyi.wms.service.IWmsCustomerService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 客户信息Controller
 * 
 * @author ruoyi
 * @date 2024-12-19
 */
@RestController
@RequestMapping("/wms/customer")
public class WmsCustomerController extends BaseController
{
    @Autowired
    private IWmsCustomerService wmsCustomerService;

    /**
     * 查询客户信息列表
     */
    @PreAuthorize("@ss.hasPermi('wms:customer:list')")
    @GetMapping("/list")
    public TableDataInfo list(WmsCustomer wmsCustomer)
    {
        startPage();
        List<WmsCustomer> list = wmsCustomerService.selectWmsCustomerList(wmsCustomer);
        return getDataTable(list);
    }

    /**
     * 导出客户信息列表
     */
    @PreAuthorize("@ss.hasPermi('wms:customer:export')")
    @Log(title = "客户信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WmsCustomer wmsCustomer)
    {
        List<WmsCustomer> list = wmsCustomerService.selectWmsCustomerList(wmsCustomer);
        ExcelUtil<WmsCustomer> util = new ExcelUtil<WmsCustomer>(WmsCustomer.class);
        util.exportExcel(response, list, "客户信息数据");
    }

    /**
     * 获取客户信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('wms:customer:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(wmsCustomerService.selectWmsCustomerById(id));
    }

    /**
     * 新增客户信息
     */
    @PreAuthorize("@ss.hasPermi('wms:customer:add')")
    @Log(title = "客户信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WmsCustomer wmsCustomer)
    {
        if (!wmsCustomerService.checkCustomerCodeUnique(wmsCustomer))
        {
            return error("新增客户'" + wmsCustomer.getCustomerName() + "'失败，客户编码已存在");
        }
        wmsCustomer.setCreateBy(getUsername());
        return toAjax(wmsCustomerService.insertWmsCustomer(wmsCustomer));
    }

    /**
     * 修改客户信息
     */
    @PreAuthorize("@ss.hasPermi('wms:customer:edit')")
    @Log(title = "客户信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WmsCustomer wmsCustomer)
    {
        if (!wmsCustomerService.checkCustomerCodeUnique(wmsCustomer))
        {
            return error("修改客户'" + wmsCustomer.getCustomerName() + "'失败，客户编码已存在");
        }
        wmsCustomer.setUpdateBy(getUsername());
        return toAjax(wmsCustomerService.updateWmsCustomer(wmsCustomer));
    }

    /**
     * 删除客户信息
     */
    @PreAuthorize("@ss.hasPermi('wms:customer:remove')")
    @Log(title = "客户信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(wmsCustomerService.deleteWmsCustomerByIds(ids));
    }
}